package logic

import (
	"context"
	"encoding/json"
	"fmt"
	"manuscript-service/mysql/cache"
	"manuscript-service/mysql/models"
	"time"

	"manuscript-service/manrpc/internal/svc"
	"manuscript-service/manrpc/manrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GaoListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGaoListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GaoListLogic {
	return &GaoListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GaoListLogic) GaoList(in *manrpc.GaoListRequest) (*manrpc.GaoListResponse, error) {
	// todo: add your logic here and delete this line
	var user models.GaoFenList
	var likes []*manrpc.GaoList
	key := fmt.Sprintf("GaoList_%s", in)
	redisData, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	if redisData == "" {
		var err error
		likes, err = user.GaoListss(models.DB, likes, in)
		if err != nil {
			return nil, err
		}
		jsonData, _ := json.Marshal(likes)
		cache.Rdb.Set(cache.Ctx, key, jsonData, time.Hour*120)
	} else {
		json.Unmarshal([]byte(redisData), &likes)
	}

	return &manrpc.GaoListResponse{
		Likes: likes,
	}, nil
}
